package AmazingMaze;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.StyledEditorKit.AlignmentAction;

public class MenuFrame extends JFrame {
    public JButton startServer = new JButton("Create Server");
    public JPanel panelServer = new JPanel();
    final static String NORM = "Have Some Fun, Sam!";
    final static String HARD = "Forever, Duke!";
    final static String EASY = "I Can Win!";
    public String diff = NORM;
    public JButton difficult = new JButton("<HTML><font size=2>Difficult: "
            + diff + "</font></HTML>");
    public JLabel sizeLabel = new JLabel("Size X:"), sizeLabel2 = new JLabel(
            "Y:");
    public JTextField fieldX = new JTextField(), fieldY = new JTextField();
    
    public JLabel hint = new JLabel();
    final static String easyHint = "<HTML><font size=2> Difficult Easy. 'I Can Win!'<br>This is the simplest mode: without monsters, hp, inventory," +
    		"and with minimum traps, much time thinking about your actions and the easiest path to exit." +
    		"for those who want to find one's feet and understand the rules of the game.</font></HTML>";
    final static String normHint = "<HTML><font size=2> Difficult Normal. 'Have some fun, Sam!'<br>Difficult for serious guys. For those who will not " +
    		"call mom and who have not the time to think about. With heals, traps, items and Dark Side of the Power." +
    		"Generally for nerds, freaks and manchkins.</font></HTML>";
    final static String hardHint = "<HTML><font size=2> Difficult Hard. 'Forever, Duke!'<br>" +
    		"If you afraid of the dark, the spiders or the large enclosed spaces you would not have to play in this difficult." +
    		"For those who are tired of wandering alone and who have a lot of time.</font></HTML>";
    
    public JButton create = new JButton("Create");
    public JButton joinServer = new JButton("Join Server");
    public JButton settings = new JButton("Settings");
    public JButton exit = new JButton("Exit");
    
    public JPanel panelSettings = new JPanel();

    public MenuFrame() {
        super();
        setLayout(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setTitle("Amazing Maze!");
        setLocation(500, 200);
        setSize(300, 500);
        setMinimumSize(new Dimension(300, 300));

        this.add(startServer);
        this.add(panelServer);
        this.add(joinServer);
        this.add(settings);
        this.add(exit);
        this.add(panelSettings);

        startServer.setBounds(10, 100, getWidth()-35, 30);
        joinServer.setBounds(10, startServer.getY()+startServer.getHeight(), getWidth()-35, 30);
        settings.setBounds(10, joinServer.getY()+joinServer.getHeight(), getWidth()-35, 30);
        exit.setBounds(10, settings.getY()+settings.getHeight(), getWidth()-35, 30);

        joinServer.setVisible(true);
        settings.setVisible(true);
        exit.setVisible(true);
        
        
        /////////////////////////PANELSERVER////////////////////////////////////
        panelServer.setBounds(10, startServer.getY() + startServer.getHeight(),
                startServer.getWidth(), 200);
        panelServer.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        panelServer.setVisible(false);
        panelServer.setLayout(null);
        
        panelServer.add(difficult);
        panelServer.add(sizeLabel);
        panelServer.add(sizeLabel2);
        panelServer.add(fieldX);
        panelServer.add(fieldY);
        panelServer.add(hint);
        panelServer.add(create);

        startServer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                if (panelServer.isVisible() != true) {
                    panelServer.setVisible(true);
                    joinServer.setBounds(10, panelServer.getY()+panelServer.getHeight()+5, getWidth()-35, 30);
                    settings.setBounds(10, joinServer.getY()+joinServer.getHeight(), getWidth()-35, 30);
                    if (panelSettings.isVisible() == true) {
                    panelSettings.setBounds(10, settings.getY() + settings.getHeight(), settings.getWidth(), 65);
                    exit.setBounds(10, panelSettings.getY()+panelSettings.getHeight(), getWidth()-35, 30);
                    } else {
                    exit.setBounds(10, settings.getY()+settings.getHeight(), getWidth()-35, 30);
                    }
                } else {
                    panelServer.setVisible(false);
                    joinServer.setBounds(10, startServer.getY()+startServer.getHeight(), getWidth()-35, 30);
                    settings.setBounds(10, joinServer.getY()+joinServer.getHeight(), getWidth()-35, 30);
                    if (panelSettings.isVisible() == true) {
                    panelSettings.setBounds(10, settings.getY() + settings.getHeight(), settings.getWidth(), 65);
                    exit.setBounds(10, panelSettings.getY()+panelSettings.getHeight(), getWidth()-35, 30);
                    } else {
                    exit.setBounds(10, settings.getY()+settings.getHeight(), getWidth()-35, 30);
                    }
                }
            }
        });

        difficult.addActionListener(new ActionListener() {            
            @Override
            public void actionPerformed(ActionEvent arg0) {
                if(diff==EASY){diff=NORM;}else{
                    if(diff==HARD){diff=EASY;}
                    if(diff==NORM){diff=HARD;}
                }
                difficult.setText("<HTML><font size=2>Difficult: "+diff+"</font></HTML>");
                if(diff==EASY){hint.setText(easyHint);}else if
                (diff==NORM){hint.setText(normHint);}else
                {hint.setText(hardHint);}
            }
        });
        
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            dispose();                
            }
        });
        
        difficult.setBounds(0,0,panelServer.getWidth(),30);
        sizeLabel.setBounds(2,30,40,20);
        fieldX.setBounds(sizeLabel.getWidth()+5,sizeLabel.getY(),(panelServer.getWidth()-sizeLabel.getWidth())/3,sizeLabel.getHeight());
        sizeLabel2.setBounds(fieldX.getX()+fieldX.getWidth()+10,fieldX.getY(),10,sizeLabel.getHeight());
        fieldY.setBounds(sizeLabel2.getX()+sizeLabel2.getWidth()+5,sizeLabel.getY(),(panelServer.getWidth()-sizeLabel.getWidth())/3,sizeLabel.getHeight());
        hint.setBounds(2,sizeLabel.getHeight()+sizeLabel.getY(),panelServer.getWidth(),120);
        create.setBounds(0, hint.getHeight()+hint.getY(), panelServer.getWidth(), 30);
        
        hint.setText(normHint);
        fieldX.setText("16");
        fieldY.setText("10");
        fieldX.addFocusListener(new FocusListener() {            
            @Override
            public void focusLost(FocusEvent arg0) {               
            if(Integer.parseInt(fieldX.getText())>84){
                fieldX.setText("84");
            }
            if(Integer.parseInt(fieldX.getText())<2){
                fieldX.setText("2");
            }
           
            }
            @Override
            public void focusGained(FocusEvent arg0) {
                fieldX.setText(" ");
            }
        });
        
        ////////////////////PANELSETTINGS///////////////
        panelSettings.setBounds(10, settings.getY() + settings.getHeight(),
                settings.getWidth(), 65);
        panelSettings.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        panelSettings.setVisible(false);
        panelSettings.setLayout(null);
        
        settings.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!panelSettings.isVisible()) {
                panelSettings.setVisible(true);
                exit.setBounds(10, panelSettings.getY()+panelSettings.getHeight(), getWidth()-35, 30);
                } else {
                panelSettings.setVisible(false);
                exit.setBounds(10, settings.getY()+settings.getHeight(), getWidth()-35, 30);
                }
            }
        });
    }
    
}
